"use strict";
/* Controllers */
app.controller("costReportDetailChartController", ['$scope', '$location', '$rootScope', '$routeParams', 'costReportDetailChartService', function ($scope, $location, $rootScope, $routeParams, costReportDetailChartService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.tag = $routeParams.tag; // [owner, cost, org]
    vm.p.ownerId = $routeParams.ownerId;
    vm.p.basicPath = $routeParams.basicPath;

    if (vm.p.basicPath != null && typeof (vm.p.basicPath) != "undefined" && vm.p.basicPath.indexOf('|') >= 0) {
        vm.p.basicPath = vm.p.basicPath.replaceAll('|', '/')
    } else {
        vm.p.basicPath = ''
    }

    if (vm.p.ownerId != null && typeof (vm.p.ownerId) != "undefined") {
        vm.p.ownerId = $routeParams.ownerId;
    }else {
        vm.p.ownerId = '0';
    }


    var errorFn = function (result) {
        alert(result);
    };

    $scope.loadCostTableReportTrend = function () {
        $("#refreshTable").addClass('disabled');
        var vm = $scope.vm;
        var params = vm.p;

        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            $scope.areaNames = data.areaNames;
            initChart(data);
            $("#refreshTable").removeClass('disabled');
        };

        costReportDetailChartService.loadCostTableReportTrend(params, procFn, errorFn);
    };

    function initChart(resultData) {
        var dom = document.getElementById("container");
        var myChart = echarts.init(dom);
        window.onresize = function () {
            myChart.resize();
        };
        var app = {};

        var option;

        option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: { type: 'cross' }
            },
            legend: {
            },
            dataZoom: [{}],
            xAxis: {
                type: 'category',
                data: resultData.xAxisData
            },
            yAxis: [
                {
                    name: '存储量' + '(' + resultData.dataUnitName + ')',
                    type: 'value'
                }
            ],
            series: [
                {
                    name: $scope.vm.p.ownerID,
                    yAxisIndex: 0,
                    data: resultData.values,
                    type: 'line',
                    smooth: false
                }
            ]
        };

        window.onresize = function () {
            myChart.resize();
        };

        if (option && typeof option === 'object') {
            myChart.setOption(option);
        }
    }


    if (vm.p.ownerId!='0') {
        $scope.loadCostTableReportTrend();
    }
}]);


/* Services */
app.service('costReportDetailChartService', ['ajaxService', function (ajaxService) {

    this.loadCostTableReportTrend = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/cost_report/loadCostTableReportTrend.do", successFunction, errorFunction);
    };

}]);

